import argparse
import os

import dota_utils
import ResultMerge
import dota_evaluation_task1
from yolo2dota import yolo2dota


def parse_opt():
    parser = argparse.ArgumentParser()
    parser.add_argument('--img-path', type=str, default='/mnt/cv/Datasets/DOTA_v1.0_v1.5/val_yolo/images',
                        help='detect image path')
    parser.add_argument('--label-path', type=str, default='runs/detect/exp1',
                        help='detect labels root path')
    opt = parser.parse_args()
    return opt


def run(img_path, label_path):
    txt_path = os.path.join(label_path, 'labels')
    des_path = os.path.join(label_path, 'labels_dota')
    result_path = os.path.join(label_path, 'result')
    result_merge_path = os.path.join(label_path, 'result_merge')
    os.makedirs(des_path, exist_ok=True)
    os.makedirs(result_path, exist_ok=True)
    os.makedirs(result_merge_path, exist_ok=True)
    yolo2dota(img_path,
              txt_path,
              des_path,
              dota_utils.wordname_15)

    dota_utils.groundtruth2Task1(des_path,
                                 result_path)

    ResultMerge.mergebypoly(result_path,
                            result_merge_path)

    detpath = os.path.join(result_merge_path, 'Task1_{:s}.txt')
    # detpath = r'../datasets/dota/result_merge/Task1_{:s}.txt'
    annopath = r'/mnt/cv/Datasets/DOTA_v1.0_v1.5/val/labelTxt/{:s}.txt'
    imagesetfile = r'/mnt/cv/Datasets/DOTA_v1.0_v1.5/val/valset.txt'
    dota_evaluation_task1.main(detpath, annopath, imagesetfile)


opt = parse_opt()
print(opt)
run(**vars(opt))
